Ext.require([
    'Ext.tree.*',
    'Ext.data.*',
    'Ext.layout.container.HBox',
    'Ext.window.MessageBox'
]);

Ext.define('treeModel', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'name'},
        {name: 'text', mapping: 'name'},
        {name: 'code'},
        {name: 'id'},
        {name: 'url'},
        {name: 'leaf'}
    ]
});

Ext.define('eleStoreTreeModel', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'name'},
        {name: 'text', mapping: 'name'},
        {name: 'code'},
        {name: 'id'},
        {name: 'url'},
        {name: 'leaf'}
    ]
});

var today = new Date();
var year = today.getFullYear();
var start_date = year + "-01-01";
var end_date = year + "-12-31";
//基础数据名称是否显示编码 0不显示 1显示
var eleNameCode = GetEleNameCode();

/*动态获取年度列表*/
function getYearList(config) {
    config=$.extend({},{start:-8,end:1},config);
    var nowYear = new Date().getUTCFullYear();//当前年份
    var startYear = nowYear +config.start;//起始年份
    var endYear = nowYear + config.end;//结束年份，默认为当前年份

    var jsonstr = "[]";
    var jsonarray = eval('(' + jsonstr + ')');
    for (var i = startYear; i <= endYear; i++) {
        var arr = {"id": i, "code": i, "name": i};
        jsonarray.push(arr);
    }
    return jsonarray;
}


/*债务基础数据，下拉框形式，从前台js获取数据,不排序方式*/
function DebtEleStoreNoSort(debtEle, params) {
    var namecode = '0';
    if (typeof params != 'undefined' && params != null) {
        namecode = params.namecode;
    }
    var debtStore = Ext.create('Ext.data.Store', {
        fields: ['id', 'code', 'name'],
//        sorters: [{
//            property: 'code',
//            direction: 'asc'
//        }],
        data: namecode == '1' ? DebtJSONNameWithCode(debtEle) : debtEle
    });
    return debtStore;
}


/*债务基础数据，下拉框形式，从前台js获取数据*/
function DebtEleStore(debtEle, params) {
    var namecode = '0';
    if (typeof params != 'undefined' && params != null) {
        namecode = params.namecode;
    }
    var debtStore = Ext.create('Ext.data.Store', {
        fields: ['id', 'code', 'name'],
        sorters: [{
            property: 'code',
            direction: 'asc'
        }],
        data: namecode == '1' ? DebtJSONNameWithCode(debtEle) : debtEle
    });
    return debtStore;
}
/*债务基础数据，下拉框形式，从后台数据库获取数据*/
function DebtEleStoreDB(debtEle, params) {
	if(!!params && !!params.condition){
		params.condition = encode64(params.condition);
	}
    var extraParams = {eleCode : debtEle};
    if (typeof params == 'object') {
    	extraParams=$.extend({},extraParams,params);
    }
    var debtStoreDB = new Ext.data.ArrayStore({
        autoLoad: true,
        fields: ['id', 'code', 'name','year'],
        sorters: [{
            property: 'code',
            direction: 'asc'
        }],
        proxy: {
            type: 'ajax',
            url: '/report/third-api/ele-data/eleValue',
            extraParams: extraParams,
            reader: {
                type: 'json',
                root: 'list'
            }
        }
    });
    return debtStoreDB;
}
function DebtEleStoreUrl(url, params) {
    var extraParams = {};
    if (typeof params == 'object') {
        extraParams = params;
    }
    var debtStoreDB = new Ext.data.ArrayStore({
        autoLoad: true,
        fields: ['id', 'code', 'name'],
        sorters: [{
            property: 'code',
            direction: 'asc'
        }],
        proxy: {
            type: 'ajax',
            url: url,
            extraParams: extraParams,
            reader: {
                type: 'json',
                root: 'list'
            }
        }
    });
    return debtStoreDB;
}
function DebtEleStoreTable(table, params) {
	if(!!params && !!params.condition){
		params.condition = encode64(params.condition);
	}
    var extraParams = {table:table};
    if (typeof params == 'object') {
    	extraParams=$.extend({},extraParams,params);
    }
    var debtStoreDB = new Ext.data.ArrayStore({
        autoLoad: true,
        fields: ['id', 'code', 'name'],
        sorters: [{
            property: 'code',
            direction: 'asc'
        }],
        proxy: {
            type: 'ajax',
            url: '/report/third-api/ele-data/table',
            extraParams: extraParams,
            reader: {
                type: 'json',
                root: 'list'
            }
        }
    });
    return debtStoreDB;
}

/*债务基础数据，下拉树形式，从前台js获取数据*/
function DebtEleTreeStoreJSON(debtEle, params) {
    var namecode = '0';
    if (typeof params != 'undefined' && params != null) {
        namecode = params.namecode;
    }
    namecode = GetEleNameCode();
    var config_store={
        model: 'eleStoreTreeModel',
        root: {
        	expaned: true,
        	children: namecode == '1' ? DebtJSONNameWithCode(debtEle) : debtEle
        },
        autoLoad: false
    };
    var debtTreeStoreDB = Ext.create('Ext.data.TreeStore', config_store);
    return debtTreeStoreDB;
}

/*债务基础数据，下拉树形式，从后台数据库获取数据*/
function DebtEleTreeStoreDB(debtEle, params,config) {
	if(!!params && !!params.condition){
		params.condition = encode64(params.condition);
	}
    var extraParams = {eleCode : debtEle};
    if (typeof params == 'object') {
    	extraParams=$.extend({},extraParams,params);
    }
    var config_store={
        model: 'eleStoreTreeModel',
        proxy: {
            type: 'ajax',
            method: 'POST',
            url: '/report/third-api/ele-data/eleValue',
            extraParams: extraParams,
            reader: {
                type: 'json'
            }
        },
        root: {name: '全部'},
        autoLoad: true
    };
    config_store=$.extend({},config_store,config);
    var debtTreeStoreDB = Ext.create('Ext.data.TreeStore', config_store);
    return debtTreeStoreDB;
}

/*债务基础数据，下拉树形式，从后台数据库获取数据*/
function DebtEleTreeStoreDBTable(table, params) {
	if(!!params && !!params.condition){
		params.condition = encode64(params.condition);
	}
    var extraParams = {table:table};
    if (typeof params == 'object') {
    	extraParams=$.extend({},extraParams,params);
    }
    var debtTreeStoreDB = Ext.create('Ext.data.TreeStore', {
        model: 'eleStoreTreeModel',
        proxy: {
            type: 'ajax',
            method: 'POST',
            url: '/report/third-api/ele-data/table',
            extraParams: extraParams,
            reader: {
                type: 'json'
            }
        },
        root: {name: '全部'},
        autoLoad: true
    });
    return debtTreeStoreDB;
}

function GetEleNameCode() {
    if (null == eleNameCode || eleNameCode.length <= 0) {
    	/*
        $.post("getDebtEleNameCode.F", function (data) {
            eleNameCode = data;
        });
        */
        eleNameCode='1';
    }

    return eleNameCode;
}

function DebtJSONNameWithCode(debtEle) {
    if (typeof debtEle == 'undefined' || debtEle == null || debtEle.length <= 0) {
        return null;
    }
    for (var o in debtEle) {
        if (debtEle[o].name.indexOf(debtEle[o].code) == -1) {
            debtEle[o].name = debtEle[o].code + " " + debtEle[o].name;
        } else {
            break;
        }
        DebtJSONNameWithCode(debtEle[o].children);
    }
    return debtEle;
}
